SUBROUTINE CPG1S

USE YOMDPHY  ,  ONLY : NCSS     ,NLEV     ,NGPP     ,NPOI     ,NTILES   ,&
     &                 NTRAC   ,NVHILO    ,NCOM     ,YDSURF   ,NCSNEC
USE YOMCDH1S  , ONLY : NLEVI  , &
     &  NDHVTLS,NDHFTLS,NDHVTSS,NDHFTSS, &
     &  NDHVTTS,NDHFTTS,NDHVTIS,NDHFTIS, &
     &  NDHVSSS,NDHFSSS,NDHVIIS,NDHFIIS, &
     &  NDHVWLS,NDHFWLS,NDHVRESS,NDHFRESS, &
     &  NDHVCO2S,NDHFCO2S, &
     &  NDHVBIOS,NDHFBIOS,NDHVBVOCS,NDHVVEGS,NDHFVEGS
 
USE YOMDYN1S , ONLY : NSTEP    ,TDT,TSTEP
USE YOMGPD1S , ONLY : VFZ0F    ,VFALBF   ,&
     &            VFALUVP  ,VFALUVD  ,VFALNIP  ,VFALNID  , &
     &            VFALUVI  ,VFALUVV  ,VFALUVG  , &
     &            VFALNII  ,VFALNIV  ,VFALNIG  , &
     &            VFITM    , &
     &            VFZ0H    ,VFCVL    ,VFCVH    ,VFCUR    ,VFTVL    ,VFTVH    , &
     &            VFLAIL   ,VFLAIH   ,VFFWET   ,VFAVGPAR ,VFRSML   ,VFRSMH   , &
     &            VFSOTY   ,VFCI     ,VFCIL, VFSST    ,VFSDOR   , &
     &            VFCO2TYP, VFISOP_EP, &
     &            VDALB    ,VDZ0F    ,VDZ0H    , &
     &            VDIEWSSTL,VDINSSSTL,VDISSHFTL,VDIETL   ,VDTSKTL,&
     &            VFLDEPTH,VFCLAKEF   ,& ! FLAKE
     &            VFZO     ,VFHO     ,VFHO_INV ,VFDO     ,VFOCDEPTH, & !KPP 
     &            VFADVT ,VFADVS  ,VFTRI0   ,VFTRI1   ,VFSWDK_SAVE,&  !KPP
     &            VDANDAYVT,VDANFMVT , &
     &            VDRESPBSTR,VDRESPBSTR2,VDBIOMASS_LAST,&
     &            VDBLOSSVT,VDBGAINVT, GPD_SDP2, GPD_SDP3

USE YOMGF1S  , ONLY : UNLEV0   ,VNLEV0   ,TNLEV0   ,QNLEV0   , CNLEV0  ,&
     &            PNLP0    ,UNLEV1   ,VNLEV1   ,TNLEV1   ,QNLEV1   , PNLP1, &
     &            CNLEV1   ,FSSRD    ,FSTRD    ,FLSRF    ,FCRF     , &
     &            FLSSF    , &
     &            FCSF     ,RALT
USE YOMGP1S0 , ONLY : GP0      ,TSLNU0   ,QLINU0   ,FSNNU0   , &
     &            TSNNU0   ,ASNNU0   ,RSNNU0   , WSNNU0,&
     &            TRENU0   ,WRENU0   ,TILNU0,&
     &            TLICENU0,TLMNWNU0,TLWMLNU0,TLBOTNU0,TLSFNU0,& ! FLAKE
     &            HLICENU0,HLMLNU0 ,&                           ! FLAKE
     &            UONU0    ,VONU0    ,TONU0    ,SONU0,&         ! KPP
!     &            UONUC    ,VONUC    ,USTRCNU  ,VSTRNUC,&       ! KPP
     &            LAINU0, BSTRNU0, BSTR2NU0
USE YOMGP1S1 , ONLY : GP1
USE PTRGP1S  , ONLY : MTSLNU   ,MQLINU   ,MFSNNU   , &
     &            MTSNNU   ,MASNNU   ,MRSNNU   , &
     &            MTRENU   ,MWRENU   ,MTILNU, & 
     &            MTLICENU,MTLMNWNU,MTLWMLNU,MTLBOTNU,MTLSFNU,& ! FLAKE
     &            MHLICENU,MHLMLNU,&                            ! FLAKE 
     &            MUONU    ,MVONU    ,MTONU    ,MSONU,&           !KPP
     &            MLAINU , MBSTRNU, MBSTR2NU,MWSNNU
USE YOMLOG1S,   ONLY: IDBGS1

USE YOMGC1S  , ONLY : GEMU     ,GELAM    ,GELAT
USE YOMCT01S , ONLY : NSTART
USE YOMCST   , ONLY : RG
USE YOERIP   , ONLY : RCODECM  ,RSIDECM  ,RCOVSRM  ,RSIVSRM
USE YOMDIM1S , ONLY : NPROMA

! USE YOS_OCEAN_ML, ONLY : LEOCML
! USE YOS_CST , ONLY : RTT

USE YOMRIP   , ONLY :  RCODEC  ,RSIDEC  ,RCOVSR  ,RSIVSR
USE YOEPHY   , ONLY :  RLAIINT

USE YOMGDI1S , ONLY : GDI1S    ,N2DDI ,&
                      &GDIAUX1S ,N2DDIAUX

USE YOS_SURF, ONLY : TSURF, GET_SURF
USE YOMLUN1S, ONLY : NULOUT
USE OMP_LIB
USE MPL_MODULE

#ifdef DOC
! (C) Copyright 1995- ECMWF.
!
! This software is licensed under the terms of the Apache Licence Version 2.0
! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
! In applying this licence, ECMWF does not waive the privileges and immunities
! granted to it by virtue of its status as an intergovernmental organisation
! nor does it submit to any jurisdiction.

!**** *CPG1S* - Grid point calculations.

!     Purpose.
!     --------
!           Grid point calculations .

!**   Interface.
!     ----------
!        *CALL* *CPG1S*

!        Explicit arguments :
!        --------------------

!        Implicit arguments :
!        --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------
!        Called by STEPO1S.

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the one column model

!     Author.
!     -------
!        Pedro Viterbo   *ECMWF*

!     Modifications.
!     --------------
!        Original : 95-03-21
!        Bart vd Hurk (KNMI): multi-column setup
!     S. Boussetta/G.Balsamo May 2010 Add CTESSEL based on:
!        Marita Voogt (KNMI) "C-Tessel" 09/2005
!        Sebastien Lafont (ECMWF) "C-TESSEL"

!     ------------------------------------------------------------------
#endif
USE PARKIND1  ,ONLY : JPIM     ,JPRB,    JPRD
USE YOMHOOK   ,ONLY : LHOOK    ,DR_HOOK, JPHOOK
IMPLICIT NONE


CHARACTER*1 :: CDCONF 
REAL(KIND=JPRB) ::  ZAPRS(NPOI,0:NLEV),ZAPHIF(NPOI)
REAL(KIND=JPRB) ::  ZDIFTQ(NPOI,0:NLEV),ZDIFTS(NPOI,0:NLEV)
REAL(KIND=JPRB) ::  ZFRSO(NPOI,0:NLEV),ZFRTH(NPOI,0:NLEV)
REAL(KIND=JPRB) ::  ZSTRTU(NPOI,0:NLEV),ZSTRTV(NPOI,0:NLEV)

REAL(KIND=JPRB) ::  ZAN(NPOI),ZAG(NPOI),ZRD(NPOI) &
     &   , ZRSOIL_STR(NPOI),ZRECO(NPOI),ZCO2FLUX(NPOI),ZCH4FLUX(NPOI),ZBVOCFLUX(NPOI,NDHVBVOCS)
REAL(KIND=JPRB) ::  ZLAI(NPOI),ZBIOM(NPOI),ZBLOSS(NPOI),ZBGAIN(NPOI) &
     &   , ZBIOMSTR(NPOI),ZBIOMSTR2(NPOI)


REAL(KIND=JPRB) ::  ZMU0M(NPOI)   ,ZEMIS(NPOI)   ,ZFTG12(NPOI)  &
     &   , ZFWEV(NPOI)   ,ZFWSB(NPOI)   ,ZFWG12(NPOI) &
     &   , ZFWMLT(NPOI)  ,ZFWROD(NPOI)  ,ZFWRO1(NPOI) &
     &   , ZFTLHEV(NPOI) ,ZFTLHSB(NPOI) ,ZVDIS(NPOI)  &
     &   , ZCFLX(NPOI,NTRAC)
REAL(KIND=JPRB) :: ZDHTLS(NPOI,NTILES,NDHVTLS+NDHFTLS) 
REAL(KIND=JPRB) :: ZDHTSS(NPOI,NCSNEC,NDHVTSS+NDHFTSS) 
REAL(KIND=JPRB) :: ZDHSSS(NPOI,NCSNEC,NDHVSSS+NDHFSSS) 
REAL(KIND=JPRB) :: ZDHTTS(NPOI,NCSS  ,NDHVTTS+NDHFTTS) 
REAL(KIND=JPRB) :: ZDHWLS(NPOI,NCSS  ,NDHVWLS+NDHFWLS) 
REAL(KIND=JPRB) :: ZDHTIS(NPOI,NLEVI ,NDHVTIS+NDHFTIS) 
REAL(KIND=JPRB) :: ZDHIIS(NPOI,       NDHVIIS+NDHFIIS)
REAL(KIND=JPRB) :: ZDHRESS(NPOI,2    ,NDHVRESS+NDHFRESS) 

REAL(KIND=JPRB) :: ZDIFM(NPOI,0:NCOM)     !KPP
REAL(KIND=JPRB) :: ZDIFT(NPOI,0:NCOM)     !KPP
REAL(KIND=JPRB) :: ZDIFS(NPOI,0:NCOM)     !KPP
REAL(KIND=JPRB) :: ZOTKE(NPOI,0:NCOM)     !TKE
REAL(KIND=JPRB) :: UONUC(NPOI,0:NCOM)     !KPP 
REAL(KIND=JPRB) :: VONUC(NPOI,0:NCOM)     !KPP
REAL(KIND=JPRB) :: USTRCNU(NPOI)          !KPP
REAL(KIND=JPRB) :: VSTRNUC(NPOI)          !KPP

REAL(KIND=JPRB) :: ZDHCO2S(NPOI,NVHILO,NDHVCO2S+NDHFCO2S)
REAL(KIND=JPRB) :: ZDHBIOS(NPOI,NVHILO,NDHVBIOS+NDHFBIOS)
REAL(KIND=JPRB) :: ZDHBVOCS(NPOI,NVHILO,NDHVBVOCS)
REAL(KIND=JPRB) :: ZDHVEGS(NPOI,NVHILO,NDHVVEGS+NDHFVEGS)

REAL(KIND=JPRB) ::  ZLAIHI(NPOI)
REAL(KIND=JPRB) ::  ZLAILI(NPOI)

REAL(KIND=JPRB) ::  ZEVAPTIU(NPOI,NTILES)

 
!    *ZU10M*        U-COMPONENT WIND AT 10 M                      M/S
!    *ZV10M*        V-COMPONENT WIND AT 10 M                      M/S
!    *ZT2M*         TEMPERATURE AT 2M                                K
!    *ZD2M*         DEW POINT TEMPERATURE AT 2M                      K
!    *ZQ2M*         SPECIFIC HUMIDITY AT 2M                       KG/KG
!    *ZMEAN*        AREA AVERAGED WIND SP. AT 10 M INT. FROM KLEV   M/S
!    *ZGUST*        GUST AT 10 M                                    M/S
!    *ZZIDLWV*      Zi/L used for gustiness in wave model           m/m
!                   (NOTE: Positive values of Zi/L are set to ZERO)
!    *ZBLH*         BOUNDARY LAYER HEIGHT                            M
REAL(KIND=JPRB) :: ZU10M(NPOI),ZV10M(NPOI),ZT2M(NPOI),ZD2M(NPOI),&
     &    ZQ2M(NPOI),ZMEAN(NPOI),ZGUST(NPOI),ZZIDLWV(NPOI),ZBLH(NPOI)
     
REAL(KIND=JPRB),ALLOCATABLE,TARGET:: ZGPE(:,:) 
REAL(KIND=JPRB), POINTER:: ZTSAE1(:,:),ZWSAE1(:,:) &
     &   , ZSNSE1(:,:)  , ZTSNE1(:,:)  , ZASNE1(:)  , ZRSNE1(:,:)  ,ZWLE1(:) &
     &   , ZTLE1(:)   ,ZTILE1(:,:),ZWSNE1(:,:)
REAL(KIND=JPRB),POINTER :: ZTLICENE1(:),ZTLMNWNE1(:),ZTLWMLNE1(:) &        ! FLAKE
     &   ,ZTLBOTNE1(:),ZTLSFNE1(:),ZHLICENE1(:),ZHLMLNE1(:)                ! FLAKE 
REAL(KIND=JPRB),POINTER :: ZUOE1(:,:),ZVOE1(:,:), ZTOE1(:,:), ZSOE1(:,:)   !KPP

REAL(KIND=JPRB), POINTER:: ZLAIE1(:,:) , ZBSTRE1(:,:) , ZBSTR2E1(:,:)

INTEGER(KIND=JPIM) :: IST,IEND,IPROMA,IFLEV, IBL, IST1,IEND1,NTHREADS
INTEGER(KIND=JPIM) :: JL,J
! INTEGER(KIND=JPIM) :: OMP_GET_MAX_THREADS

TYPE(TSURF), POINTER :: YSURF

REAL(KIND=JPHOOK)    :: ZHOOK_HANDLE

#include "callpar1s.intfb.h"
#include "upddiag.intfb.h"

IF (LHOOK) CALL DR_HOOK('CPG1S',0,ZHOOK_HANDLE)


!     ------------------------------------------------------------------

YSURF => GET_SURF(YDSURF)
ASSOCIATE(LEOCML=>YSURF%YOCEAN_ML%LEOCML,RTT=>YSURF%YCST%RTT)

! Some initialisations
ZDHRESS(:,:,:)=0._JPRB
ZDHTTS(:,:,:)=0._JPRB
ZDHVEGS(:,:,:)=0._JPRB

! Allocate variables
ALLOCATE (ZGPE(1:NPOI,NGPP))
ZTSAE1 => ZGPE(:,MTSLNU:MTSLNU+NCSS-1)
ZWSAE1 => ZGPE(:,MQLINU:MQLINU+NCSS-1)
ZSNSE1 => ZGPE(:,MFSNNU:MFSNNU+NCSNEC-1)
ZTSNE1 => ZGPE(:,MTSNNU:MTSNNU+NCSNEC-1)
ZWSNE1 => ZGPE(:,MWSNNU:MWSNNU+NCSNEC-1)
ZRSNE1 => ZGPE(:,MRSNNU:MRSNNU+NCSNEC-1)
ZASNE1 => ZGPE(:,MASNNU)
ZTLE1  => ZGPE(:,MTRENU)
ZWLE1  => ZGPE(:,MWRENU)
ZTILE1 => ZGPE(:,MTILNU:MTILNU+NCSS-1)

!* < FLAKE
ZTLICENE1 => ZGPE(:,MTLICENU)
ZTLMNWNE1 => ZGPE(:,MTLMNWNU)
ZTLWMLNE1 => ZGPE(:,MTLWMLNU)
ZTLBOTNE1 => ZGPE(:,MTLBOTNU)
ZTLSFNE1  => ZGPE(:,MTLSFNU)
ZHLICENE1 => ZGPE(:,MHLICENU)
ZHLMLNE1  => ZGPE(:,MHLMLNU)
!* FLAKE > 

ZUOE1 => ZGPE(:,MUONU:MUONU+(NCOM+1)-1)  !KPP
ZVOE1 => ZGPE(:,MVONU:MVONU+(NCOM+1)-1)  !KPP
ZTOE1 => ZGPE(:,MTONU:MTONU+(NCOM+1)-1)  !KPP
ZSOE1 => ZGPE(:,MSONU:MSONU+(NCOM+1)-1)  !KPP
UONUC(:,:)=0.0_JPRB     !KPP 
VONUC(:,:)=0.0_JPRB     !KPP
USTRCNU(:)=0.0_JPRB     !KPP
VSTRNUC(:)=0.0_JPRB     !KPP

!* < CTESSEL
ZLAIE1 => ZGPE(:,MLAINU:MLAINU+NVHILO-1)
ZBSTRE1 => ZGPE(:,MBSTRNU:MBSTRNU+NVHILO-1)
ZBSTR2E1 => ZGPE(:,MBSTR2NU:MBSTR2NU+NVHILO-1)
!* CTESSEL >



!*       1.    INITIAL COMPUTATIONS.
!              ---------------------


!     TIME STEP




!*       1.1  INITIALIZING SURFACE VARIABLES AT T-DT (FIRST TIMESTEP).
!             --------------------------------------------------------    

! --- original setting without openMP
!IST=1
!IEND=NPOI
!IPROMA=NPOI
! ---
IFLEV=NLEV
!$OMP PARALLEL
NTHREADS=OMP_GET_MAX_THREADS()
!$OMP END PARALLEL 

IF ( IDBGS1 > 1 ) THEN
  WRITE(NULOUT,*) 'CPG1S:',NPROMA,NPOI,NTHREADS
ENDIF

!$OMP PARALLEL DO SCHEDULE(DYNAMIC,1) PRIVATE(IST,IEND,IBL,IPROMA,IST1,IEND1)
DO IST = 1, NPOI, NPROMA
  IEND = MIN(IST+NPROMA-1,NPOI)
  IBL = (IST-1)/NPROMA + 1
  IPROMA = IEND-IST+1
  IST1 = 1
  IEND1 = IPROMA

  GP1(IST:IEND,1:NGPP)=GP0(IST:IEND,1:NGPP)

!     STAFF FOR CALLPAR1S

  ZAPHIF(IST:IEND)=RG*RALT
  ZAPRS(IST:IEND,IFLEV)=PNLP0(IST:IEND)
  ZAPRS(IST:IEND,IFLEV-1)=PNLP0(IST:IEND)-RALT*0.0065_JPRB
! Added "MIN(1._JPRB , ... ) to computation of ZMU0M. This ensures ZMU0M is between physical boundaries.
  ZMU0M(IST:IEND)=MIN(1._JPRB, MAX( RSIDECM*GEMU(IST:IEND) &
     & -RCODECM*RCOVSRM*SQRT(1._JPRB-GEMU(IST:IEND)**2)*COS(GELAM(IST:IEND)) &
     & +RCODECM*RSIVSRM*SQRT(1._JPRB-GEMU(IST:IEND)**2)*SIN(GELAM(IST:IEND)) &
     & ,0._JPRB) )

  IF(LEOCML) THEN
! replace SST with the top layer temp. of the ocean mixed layer (~ 1m depth)
    VFSST(IST:IEND) = TONU0(IST:IEND,1) + RTT ! The unit of PSST(VFSST) is K.  
  ENDIF

!! init 
  ZCFLX(:,:) = 0._JPRB 

!*      2.   CALL PHYSICS.
!            -------------
  CDCONF='F'
  CALL CALLPAR1S (CDCONF &
     & , IST1, IEND1   , IPROMA , NCSS  , NTILES, NVHILO & 
     & , IFLEV  ,NSTART ,NSTEP  ,NTRAC   &
     & , NLEVI  ,NCOM   ,NCSNEC          &                 !KPP
     & , NDHVTLS,NDHFTLS,NDHVTSS,NDHFTSS &
     & , NDHVTTS,NDHFTTS,NDHVTIS,NDHFTIS &
     & , NDHVSSS,NDHFSSS,NDHVIIS,NDHFIIS &
     & , NDHVWLS,NDHFWLS,NDHVRESS,NDHFRESS &
     & , NDHVCO2S,NDHFCO2S &
     & , NDHVBIOS,NDHFBIOS,NDHVBVOCS,NDHVVEGS,NDHFVEGS  &
     & , TDT &
!-----------------------------------------------------------------------
! - INPUT .
     & , GPD_SDP2(IST:IEND,:), GPD_SDP3(IST:IEND,:,:), ZAPHIF(IST:IEND) , ZAPRS(IST:IEND,:) &
     & , UNLEV0(IST:IEND) , VNLEV0(IST:IEND) , TNLEV0(IST:IEND) , QNLEV0(IST:IEND), CNLEV0(IST:IEND,:) &
     & , FSNNU0(IST:IEND,:) &
     & , ASNNU0(IST:IEND) , RSNNU0(IST:IEND,:) , TSNNU0(IST:IEND,:),WSNNU0(IST:IEND,:) &
     & , TSLNU0(IST:IEND,:) , WRENU0(IST:IEND) , TRENU0(IST:IEND) , QLINU0(IST:IEND,:) &
     & , TILNU0(IST:IEND,:) &
     & , TLICENU0(IST:IEND),TLMNWNU0(IST:IEND),TLWMLNU0(IST:IEND),TLBOTNU0(IST:IEND),TLSFNU0(IST:IEND) &
     ! FLAKE
     & , HLICENU0(IST:IEND),HLMLNU0(IST:IEND),VFLDEPTH(IST:IEND),VFCLAKEF(IST:IEND) &   ! FLAKE 
     & , VFALBF(IST:IEND) &
     & , VFALUVP(IST:IEND),VFALUVD(IST:IEND) ,VFALNIP(IST:IEND) ,VFALNID(IST:IEND) &
     & , VFALUVI(IST:IEND),VFALUVV(IST:IEND) ,VFALUVG(IST:IEND) &
     & , VFALNII(IST:IEND),VFALNIV(IST:IEND) ,VFALNIG(IST:IEND) &
     & , VFCVL(IST:IEND)  , VFCVH(IST:IEND)  , VFCUR(IST:IEND) , VFTVL(IST:IEND)  , VFCO2TYP(IST:IEND), VFISOP_EP(IST:IEND), VFTVH(IST:IEND)  &
     & , VFLAIL(IST:IEND) , VFLAIH(IST:IEND) , VFFWET(IST:IEND) , VFAVGPAR(IST:IEND), VFRSML(IST:IEND) , VFRSMH(IST:IEND) &
     & , VFSOTY(IST:IEND) , VFSDOR(IST:IEND) &
     & , VFCI(IST:IEND)   , VFCIL(IST:IEND), VFSST(IST:IEND)   , GEMU(IST:IEND),  GELAT(IST:IEND), ZCFLX(IST:IEND,:) &
     & , VDIEWSSTL(IST:IEND,:),VDINSSSTL(IST:IEND,:),VDISSHFTL(IST:IEND,:),VDIETL(IST:IEND,:)   ,VDTSKTL(IST:IEND,:) &
     & , VDANDAYVT(IST:IEND,:),VDANFMVT(IST:IEND,:) &
     & , VDRESPBSTR(IST:IEND,:),VDRESPBSTR2(IST:IEND,:),VDBIOMASS_LAST(IST:IEND,:),BSTRNU0(IST:IEND,:),BSTR2NU0(IST:IEND,:) & 
     & , VDBLOSSVT(IST:IEND,:),VDBGAINVT(IST:IEND,:) &
     & , VFITM(IST:IEND) &
     & , VFZ0F(IST:IEND)  , ZMU0M(IST:IEND) &
     & , VFZ0H(IST:IEND) &
     & , VFZO(IST:IEND,:)     ,VFHO(IST:IEND,:)     ,VFHO_INV(IST:IEND,:)  ,VFDO(IST:IEND,:)      ,VFOCDEPTH(IST:IEND)  &!KPP
     & , VFADVT(IST:IEND,:)   ,VFADVS(IST:IEND,:)   ,VFTRI0(IST:IEND,:)    ,VFTRI1(IST:IEND,:)    ,VFSWDK_SAVE(IST:IEND,:)&!KPP
!     & , UONUC    ,VONUC    ,USTRCNU   ,VSTRNUC               &!KPP
     & , UONU0(IST:IEND,:)    ,VONU0(IST:IEND,:)    ,TONU0(IST:IEND,:)     ,SONU0(IST:IEND,:)                 &!KPP
! - INPUT AT T+1, AND/OR ATMOSPHERIC FORCING
     & , UNLEV1(IST:IEND) , VNLEV1(IST:IEND) , TNLEV1(IST:IEND) , QNLEV1(IST:IEND) , CNLEV1(IST:IEND,:) &
     & , FSSRD(IST:IEND)  , FSTRD(IST:IEND) &
     & , FCRF(IST:IEND)   , FCSF(IST:IEND)   , FLSRF(IST:IEND)  , FLSSF(IST:IEND) &
! - OUTPUT .
     & , ZEVAPTIU(IST:IEND,:)&  
     &  ,ZU10M(IST:IEND)  ,ZV10M(IST:IEND)   ,ZT2M(IST:IEND)    ,ZD2M(IST:IEND)  &
     &  ,ZQ2M(IST:IEND)   ,ZMEAN(IST:IEND)   ,ZGUST(IST:IEND)   ,ZZIDLWV(IST:IEND)   ,ZBLH(IST:IEND)  &
     & , ZDIFTQ(IST:IEND,:) , ZDIFTS(IST:IEND,:) &
     & , ZFRSO(IST:IEND,:) &
     & , ZFRTH(IST:IEND,:) &
     & , ZSTRTU(IST:IEND,:) , ZSTRTV(IST:IEND,:) &
     & , ZAN(IST:IEND), ZAG(IST:IEND), ZRD(IST:IEND), ZRSOIL_STR(IST:IEND), ZRECO(IST:IEND), ZCO2FLUX(IST:IEND),ZCH4FLUX(IST:IEND) &
     & , ZBVOCFLUX(IST:IEND,:) &
     & , ZLAI(IST:IEND),LAINU0(IST:IEND,1),LAINU0(IST:IEND,2), ZBIOM(IST:IEND), ZBLOSS(IST:IEND), ZBGAIN(IST:IEND), &
           & ZBIOMSTR(IST:IEND), ZBIOMSTR2(IST:IEND) &
     & , VDALB(IST:IEND) &
     & , ZFTG12(IST:IEND) , ZFWEV(IST:IEND)  , ZFWSB(IST:IEND)  , ZFWG12(IST:IEND) , ZFWMLT(IST:IEND) , VDZ0F(IST:IEND)  , &
           & VDZ0H(IST:IEND) &
     & , ZFWROD(IST:IEND) , ZFWRO1(IST:IEND) , ZFTLHEV(IST:IEND), ZFTLHSB(IST:IEND) &
     & , ZVDIS(IST:IEND) &
     & , ZDIFM(IST:IEND,:)  , ZDIFT(IST:IEND,:)  , ZDIFS(IST:IEND,:), ZOTKE(IST:IEND,:) &             !KPP/TKE
     & , ZSNSE1(IST:IEND,:) , ZASNE1(IST:IEND) , ZRSNE1(IST:IEND,:) , ZTSNE1(IST:IEND,:),ZWSNE1(IST:IEND,:) &
     & , ZTSAE1(IST:IEND,:) &
     & , ZWLE1(IST:IEND)  , ZTLE1(IST:IEND)  , ZWSAE1(IST:IEND,:) &
     & , ZTILE1(IST:IEND,:) &
     & , ZTLICENE1(IST:IEND),ZTLMNWNE1(IST:IEND),ZTLWMLNE1(IST:IEND)&          ! FLAKE 
     & , ZTLBOTNE1(IST:IEND),ZTLSFNE1(IST:IEND),ZHLICENE1(IST:IEND),ZHLMLNE1(IST:IEND) & ! FLAKE 
     & , ZUOE1(IST:IEND,:)  , ZVOE1(IST:IEND,:)  , ZTOE1(IST:IEND,:)  , ZSOE1(IST:IEND,:) &            !KPP
     & , ZLAIE1(IST:IEND,:), ZBSTRE1(IST:IEND,:), ZBSTR2E1(IST:IEND,:) &
     & , ZDHTLS(IST:IEND,:,:),ZDHTSS(IST:IEND,:,:),ZDHTTS(IST:IEND,:,:),ZDHTIS(IST:IEND,:,:),ZDHIIS(IST:IEND,:), &
           & ZDHSSS(IST:IEND,:,:),ZDHWLS(IST:IEND,:,:),ZDHRESS(IST:IEND,:,:) &
     & , ZDHCO2S(IST:IEND,:,:),ZDHBVOCS(IST:IEND,:,:),ZDHBIOS(IST:IEND,:,:),ZDHVEGS(IST:IEND,:,:))

!*           UPDATE DIAGNOSTICS
!     ------------------------------------------------------------------

  CALL UPDDIAG( &
   & IST1,IEND1,IPROMA,IFLEV,NCSS,NCSNEC,NTILES,NVHILO,&   
   & NDHVTLS,NDHFTLS,NDHVTSS,NDHFTSS, &
   & NDHVTTS,NDHFTTS,NDHVSSS,NDHFSSS, &
   & NDHVIIS,NDHFIIS,NDHVWLS,NDHFWLS, &
   & NDHVRESS,NDHFRESS, &
   & NDHVCO2S,NDHFCO2S,NDHVBIOS,NDHFBIOS, &
   & NDHVVEGS,NDHFVEGS,NDHVBVOCS, &
   & TDT,IST,IEND, &
   & ZEVAPTIU(IST:IEND,:),&
   & ZAN(IST:IEND),ZAG(IST:IEND),ZRD(IST:IEND),ZRSOIL_STR(IST:IEND),ZRECO(IST:IEND),ZCO2FLUX(IST:IEND),ZCH4FLUX(IST:IEND), ZBVOCFLUX(IST:IEND,:), &
   & ZLAI(IST:IEND),ZBIOM(IST:IEND),ZBLOSS(IST:IEND),ZBGAIN(IST:IEND),ZBIOMSTR(IST:IEND),ZBIOMSTR2(IST:IEND), &
   & TNLEV1(IST:IEND),QNLEV1(IST:IEND),UNLEV1(IST:IEND),VNLEV1(IST:IEND),PNLP1(IST:IEND),&
   & ZFRSO(IST:IEND,:),FSSRD(IST:IEND),ZFRTH(IST:IEND,:),FSTRD(IST:IEND),VDALB(IST:IEND),ZFTLHEV(IST:IEND),ZFTLHSB(IST:IEND), &
   & ZDIFTQ(IST:IEND,:),ZDIFTS(IST:IEND,:),ZSTRTU(IST:IEND,:),ZSTRTV(IST:IEND,:),ZWSAE1(IST:IEND,:),ZSNSE1(IST:IEND,:), &
       & ZWLE1(IST:IEND), &
   & FCRF(IST:IEND)    ,FCSF(IST:IEND)   ,FLSRF(IST:IEND),FLSSF(IST:IEND), ZFWRO1(IST:IEND),ZFWROD(IST:IEND),ZFWMLT(IST:IEND),&
   & ZDHTLS(IST:IEND,:,:),ZDHTSS(IST:IEND,:,:),ZDHTTS(IST:IEND,:,:),ZDHSSS(IST:IEND,:,:),ZDHIIS(IST:IEND,:),ZDHWLS(IST:IEND,:,:), &
       & ZDHRESS(IST:IEND,:,:),&
   & ZT2M(IST:IEND),ZD2M(IST:IEND),ZDHCO2S(IST:IEND,:,:),ZDHBIOS(IST:IEND,:,:),ZDHVEGS(IST:IEND,:,:),&
   & ZDHBVOCS(IST:IEND,:,:))
 

!*       3.  COMPUTATION OF T+DT VALUES FOR SURFACE VARIABLES.
!            -------------------------------------------------
! WARM START FIX
  IF (NSTEP == 0) THEN 
!    GP1(IST:IEND, MRSNNU:MTSNNU+NCSNEC-1)  = RSNNU0(IST:IEND, 1:NCSNEC) 
!    GP1(IST:IEND, MTSNNU:MTSNNU+NCSNEC-1 ) = TSNNU0(IST:IEND, 1:NCSNEC)
!    GP1(IST:IEND, MFSNNU:MTSNNU+NCSNEC-1)  = FSNNU0(IST:IEND, 1:NCSNEC) 
!    GP1(IST:IEND, MWSNNU:MWSNNU+NCSNEC-1)  = WSNNU0(IST:IEND, 1:NCSNEC) 
    GP1(IST:IEND, MRSNNU:MRSNNU+NCSNEC-1)  = RSNNU0(IST:IEND, 1:NCSNEC) 
    GP1(IST:IEND, MTSNNU:MTSNNU+NCSNEC-1 ) = TSNNU0(IST:IEND, 1:NCSNEC)
    GP1(IST:IEND, MFSNNU:MFSNNU+NCSNEC-1)  = FSNNU0(IST:IEND, 1:NCSNEC) 
    GP1(IST:IEND, MWSNNU:MWSNNU+NCSNEC-1)  = WSNNU0(IST:IEND, 1:NCSNEC) 
  END IF


  GP1(IST:IEND,1:NGPP)=GP1(IST:IEND,1:NGPP)+ &
       &                TDT*ZGPE(IST:IEND,1:NGPP)
!*       4.  SWAPPING OF SURFACE VARIABLES.
!            ------------------------------

  GP0(IST:IEND,1:NGPP)=GP1(IST:IEND,1:NGPP)

!*       5.   ACCUMULATE 1D DIAGNOSTIC TENDENCIES.
!             ------------------------------------

  DO J=1,N2DDI
    GDI1S(IST:IEND,J,2)=GDI1S(IST:IEND,J,2)+TSTEP*GDI1S(IST:IEND,J,1)
  ENDDO

!*       6.   AVERAGE AUXILIARY DIAGNOSTICS
!             ------------------------------------

  DO J=1,N2DDIAUX
    GDIAUX1S(IST:IEND,J,2)=GDIAUX1S(IST:IEND,J,2)+&
     &TSTEP*GDIAUX1S(IST:IEND,J,1)
  ENDDO

ENDDO !IST LOOP
!$OMP END PARALLEL DO 


!     ------------------------------------------------------------------

DEALLOCATE (ZGPE)
END ASSOCIATE
IF (LHOOK) CALL DR_HOOK('CPG1S',1,ZHOOK_HANDLE)
RETURN
END SUBROUTINE CPG1S
